package com.study.controller.user;

import com.study.constant.MessageConstant;
import com.study.result.Result;
import com.study.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


/**
 * 用户通用接口
 */
@RestController
@RequestMapping("/user/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
    @Autowired
    AliOssUtil aliOssUtil;

    /**
     * 文件上传 aliOss文件存储
     *
     * @param file
     * @return
     */
/*
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) {
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID() + extension;
            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败{}", e.getMessage());
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
*/

    /**
     * 文件上传 服务器本地文件存储
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) {
        //判断文件是否为空
        if(file == null){
            return Result.error(MessageConstant.UPLOAD_FAILED);
        }
        //存储服务器的地址端口(图片访问路径前缀)
        String serverPath = "http://62.234.165.233:9001/img/user/upload/";
        //String serverPath = "http://localhost:9000/";

        //文件真实存储路径
        //Windows
        //String fileStoragePath = "D:\\studySystem\\upload\\";
        //Linux
        String fileStoragePath = "/www/wwwroot/img/user/upload/";

        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID() + extension;
            //文件的保存路径
            String filePath = fileStoragePath + objectName;
            //将文件保存到服务器本地
            file.transferTo(new File(filePath));
            //文件请求路径，启动一个nginx端口做图片回显
            String fileRequestPath = serverPath + objectName;
            //返回文件请求路径
            return Result.success(fileRequestPath);
        } catch (IOException e) {
            log.error("文件上传失败{}", e.getMessage());
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

}
